<?php
/**
 * member_user表model
 * 
 * @author sz 2016-8-31 09:46:20
 * @version 1.0
 */
namespace Home\Model;
use Think\Model;

class MemberUserModel extends Model
{

    /**
     * model对应表和数据库信息
     *
     * @author sz 2016-8-31 13:30:24
     * @version 1.0
     */
    function __construct ()
    {
        parent::__construct('member_user', null, 'HAICHUANG');
    }

    /**
     * checkMember
     *
     * @param            
     *
     *
     * @return 添加成功true，失败返回false
     * @author sz 2016-8-31 13:32:17
     * @version 1.0
     */
    public function checkMember ($user_name)
    {
        return $this->field('id,salt')
            ->where("user_name='$user_name' AND status='1'")
            ->select();
    }
    
    /**
     * checkAllMember
     *
     * @param
     *
     *
     * @return 添加成功true，失败返回false
     * @author sz 2016-9-19 15:26:25
     * @version 1.0
     */
    public function checkAllMember ($user_name)
    {
        return $this->field('id')
        ->where("user_name='$user_name'")
        ->select();
    }

    /**
     * login
     *
     * @param            
     *
     * @return 添加成功true，失败返回false
     * @author sz 2016-8-31 13:32:48
     * @version 1.0
     */
    public function login ($account, $password)
    {        
        $sql = "SELECT m.id,m.user_name,m.real_name,m.group_id,m.avatar,g.name AS group_name FROM member_user m
                LEFT JOIN group_info g ON g.id=m.group_id 
                WHERE m.user_name='$account' AND m.password='$password' AND m.status='1'";
        return $this->query($sql);
    }

    /**
     * getUserInfo
     *
     * @param $user_name  
     * @param $page          
     *
     * @return 查询用户数据
     * @author sz 2016-9-13 14:22:19
     * @version 1.0
     */
    public function getUserInfo ($user_name, $page)
    {
        $sql = "SELECT m.id,m.user_name,m.real_name,m.group_id,m.avatar,m.status,m.update_time,g.name AS group_name FROM member_user m 
                LEFT JOIN group_info g ON g.id=m.group_id WHERE 1=1";
        if ($user_name != null)
        {
            $sql .= " AND m.real_name like '%$user_name%'";
        }
        $sql .= " ORDER BY m.status DESC,m.update_time DESC";
        if ($page != null)
        {
            $sql .= " LIMIT $page->firstRow, $page->listRows";
        }
        return $this->query($sql);
    }
    
    /**
     * getUserById
     *
     * @param $id
     *
     * @return 查询用户数据
     * @author sz 2016-9-14 14:32:55
     * @version 1.0
     */
    public function getUserById ($id)
    {
        $sql = "SELECT * FROM member_user WHERE id=" . $id;
        return $this->query($sql);
    }
    
    /**
     * updateStatus
     *
     * @param
     *
     * @return 删除成功true，失败返回false
     * @author sz 2016-9-14 09:04:35
     * @version 1.0
     */
    public function updateStatus ($id, $status)
    {
        $data['status'] = $status;
        return $this->where("id=$id")->save($data);
    }

    /**
     * add
     *
     * @param
     *
     * @return 添加成功true，失败返回false
     * @author sz 2015-12-9 15:09:25
     * @version 1.0
     */
    public function addUser($user_name, $real_name, $edsPassword, $group_id, $cellphone, $avatar, $salt, $regip)
    {
        $dataArray = array();
        $dataArray['user_name'] = $user_name;
        $dataArray['real_name'] = $real_name;
        $dataArray['password'] = $edsPassword;
        $dataArray['group_id'] = $group_id;
        $dataArray['cellphone'] = $cellphone;
        $dataArray['salt'] = $salt;
        $dataArray['reg_ip'] = $regip;
         
        if($avatar != null)
        {
            $dataArray['avatar'] = $avatar;
        }
         
        $dataArray['create_time'] = array(
                'exp',
                'now()'
        );
        return $this->add($dataArray);
    }
    
    /**
     * modifyUser
     *
     * @param
     *
     * @return 修改成功true，失败返回false
     * @author sz 2015-12-9 15:09:25
     * @version 1.0
     */
    public function modifyUser($user_id, $real_name, $edsPassword, $group_id, $cellphone, $avatar, $salt)
    {
        $dataArray = array();
        $dataArray['real_name'] = $real_name;
        $dataArray['group_id'] = $group_id;
        $dataArray['cellphone'] = $cellphone;
       
        if($avatar != null)
        {
            $dataArray['avatar'] = $avatar;
        }
        if($edsPassword != null)
        {
            $dataArray['password'] = $edsPassword;
            $dataArray['salt'] = $salt;
        }
         
        return $this->where("id=$user_id")->save($dataArray);
    }
    
    /**
     * modifyPwd
     *
     * @param
     *
     * @return 修改成功true，失败返回false
     * @author sz 2016-9-28 14:17:23
     * @version 1.0
     */
    public function modifyPwd($user_id, $new_password, $salt)
    {
        $dataArray = array();
        $dataArray['password'] = $new_password;
        $dataArray['salt'] = $salt;
        return $this->where("id=$user_id")->save($dataArray);
    }
    
    /**
     * del
     *
     * @param            
     *
     *
     * @return 删除成功true，失败返回false
     * @author sz 2016-8-31 13:33:09
     * @version 1.0
     */
    public function del ($id)
    {
        $where = array();
        $where['id'] = array('in', $id);
        return $this->where($where)->delete();
    }
}
?>